import 'package:flutter/material.dart';
import 'package:dio/dio.dart';

void main() {
  runApp(TestPage());
}

class TestPage extends StatefulWidget {
  @override
  _TestPageState createState() => _TestPageState();
}

class _TestPageState extends State<TestPage> {
  late List formList;

  initState() {
    super.initState();
    getHttp().then((val) {
      setState(() {
        formList = val['result'].toList();
      });
    });
  }

  Future getHttp() async {
    try {
      Response response;
      Dio dio = new Dio();
      response = await dio.get(
          "https://mock.mengxuegu.com/mock/604b722bf340b05bceda401a/example/tvlist");
      print(response);
      return response.data;
    } catch (e) {
      return print(e);
    }
  }

  Widget buildGrid() {
    List<Widget> tiles = []; //先建一个数组用于存放循环生成的widget
    for (var item in formList) {
      tiles.add(new Column(children: <Widget>[
        Image.network(
          item['image'],
          width: 500,
          height: 100,
          fit: BoxFit.cover,
        ),
        Text(item['title'])
      ]));
    }
    return Column(children: tiles);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('循环渲染组件案例'),
        ),
        body: SingleChildScrollView(
            child: Column(
          children: <Widget>[
            buildGrid(),
          ],
        )));
  }
}
